<!--电子围栏页 GeofenceView.vue-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('geofenceMap')">新建围栏</el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="goSearchForm.name"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;">
        <el-select placeholder="请选择" v-model="goSearchForm.status">
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadGeo">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
    <el-table stripe :data="genTableData">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true" width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" v-model="scope.row.status" @change="changeStatus(scope.row.id,scope.row.status)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="loadVehicle(scope.row.id)">管理车辆</el-button>
          <el-button type="primary" link @click="deleteGeo(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="AddBindVehicle">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button  link type="primary" @click="deletebyid(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible">
    <el-table :data="AddBindVehicleArr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="AddBindVehicleNew(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";
import router from "@/router";

//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);

//-----------------------------------------
//查询列表
//查询条件
const goSearchForm=ref({name:'',status:''});
//保存查询结果
const genTableData=ref([]);
//定义加载围栏的方法
const  loadGeo=()=>{
  console.log('加载围栏',goSearchForm.value);
  let data = qs.stringify(goSearchForm.value);
  axios.get(BASE_URL+'/v1/geofence/select?'+data).
  then((response)=>{
    if(response.data.code==2000){
      genTableData.value=response.data.data;
      console.log(genTableData.value);
    }else{
      ElMessage.error(response.data.msg);
    }
})
}
//定义重置搜索条件的方法
const resetSearch=()=>{
  goSearchForm.value={name:'',status:''};
  loadGeo();
}
//刷新界面
onMounted(()=>{
  loadGeo();
})
//-----------------------------------------
const bindVehicleArr = ref([]);
var id1;
//管理车辆
const loadVehicle=(id)=>{
  id1=id;
  console.log('管理车辆',id);
  bindVehicleDialogVisible.value=true;
  axios.get(BASE_URL+'/v1/geofence/vehicle?geofenceId='+id).
  then((response)=>{
    if(response.data.code==2000){
      bindVehicleArr.value=response.data.data;
      console.log(response.data.data);
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//----------------------------------------------
const deletebyid=(id)=>{
  console.log('移除',id);
  axios.post(BASE_URL+'/v1/geofence/vehicle/updata?geofenceId='+id).
  then((response)=>{
    if(response.data.code==2000){
      ElMessage.success("移除成功");
      loadVehicle(id1);
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//--------------------------------------------------
//添加可绑定车辆列表
const AddBindVehicleArr = ref([]);
const AddBindVehicle=()=>{
  AddBindVehicleDialogVisible.value=true;
  axios.get(BASE_URL+'/v1/vehicle/list?geofenceBindStatus=0').
  then((response)=>{
    if(response.data.code==2000){
      AddBindVehicleArr.value=response.data.data;
      console.log(response.data.data);
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//--------------------------------------------------
const AddBindVehicleNew=(id)=>{
  console.log('添加绑定车辆',id);
  axios.post(BASE_URL+'/v1/geofence/vehicle/updatanew?id='+id+'&geofenceId='+id1).
  then((response)=>{
    if(response.data.code==2000){
      ElMessage.success("添加成功");
      AddBindVehicle();
      loadVehicle(id1);
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//--------------------------------------------------
//删除围栏
const deleteGeo=(id)=>{
  console.log('删除围栏',id);
  axios.post(BASE_URL+'/v1/geofence/delete?id='+id).
  then((response)=>{
    if(response.data.code==2000){
      ElMessage.success('删除成功!');
      loadGeo();//删除成功后,重新加载数据
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//--------------------------------------------------
const changeStatus=(id,status)=>{
  console.log('修改状态',id,status);
  axios.post(BASE_URL+'/v1/geofence/updataGeofence?id='+id+'&status='+status).
  then((response)=>{
    if(response.data.code==2000){
      ElMessage.success('修改成功!');
      loadGeo();//修改成功后,重新加载数据
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
</script>

<style scoped>
</style>